package com.chuiyuan.study;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by chuiyuan on 3/9/16.
 */
public class LockTest {
    public static void main(String [] args) {
        //AtomicInteger
        final Outputter outputter = new Outputter();
        new Thread(){
            public void run(){
                outputter.output("zhangsan");
            }
        }.start();
        new Thread(){
            public void run(){
                outputter.output("lishi");
            }
        }.start();
    }
}

class Outputter{
    private Lock lock = new ReentrantLock();//
    public void output(String name){
        lock.lock();
        try {
            for (int i=0;i<name.length();i++){
                System.out.println(name.charAt(i));
            }
        }finally {
            lock.unlock();
        }
    }
}
